package com.share.web.action.sysmgr.privilege;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.share.bean.sysmgr.privilege.PrivilegeResource;
import com.share.bean.sysmgr.privilege.PrivilegeResourceItem;
import com.share.service.sysmgr.privilege.PrivilegeResourceItemManager;
import com.share.service.sysmgr.privilege.PrivilegeResourceManager;
import com.share.web.action.base.BaseActionSupport;


/**
 * <p>
 * Title：资源保护Action
 * </p>
 * <p>
 * Description：
 * </p>
 * 
 * @Author 杨浩泉
 * @Date 2010-3-11
 */
@Component
@Scope("prototype")
public class PrivilegeResAction extends BaseActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private PrivilegeResource res;

	@Resource
	private PrivilegeResourceItemManager resItemMgr;

	@Resource
	private PrivilegeResourceManager resMgr;
	
	private int resItemId ;
	
	public int getResItemId() {
		return resItemId;
	}

	public void setResItemId(int resItemId) {
		this.resItemId = resItemId;
	}

	public String addBeforeQueryResItem() throws Exception {
		if(null != this.getBaseform().getParam()) {
			this.getBaseform().setParam(new String(this.getBaseform().getParam().getBytes("ISO-8859-1"),"UTF-8")) ;
		}
		request.setAttribute("resItem", this.resItemMgr
				.queryResourceItemManager());
		
		
		if("addBeforeSel".equals(this.getBaseform().getType())) {
			return "addBeforeSel";
		} else {
			return "addBefore";
		}
		
	}

	public String addResource() throws Exception {
		resItemId = res.getResId() ;
		this.resMgr.saveResourceManager(res);

		return "queryByIdItem" ;
	}
	
	public String addResourceSel() throws Exception {
		System.out.println(this.getBaseform().getParam());
		
		this.resMgr.saveResourceManager(res.getResId(), this.getBaseform().getParam()) ;

		return "pub_add_success" ;
	}
	
	public String queryByIdResource() throws Exception {
		request.setAttribute("resItem", this.resItemMgr.queryResourceItemManager());
		request.setAttribute("queryByIdResource", this.resMgr.queryByIdResourceManager(res.getResId())) ;

		return "queryByIdResource";
	}
	
	public String deleteByIdResource() throws Exception {
		this.resMgr.deleteResourceManager(res.getResId()) ;

		return "deleteByIdResource";
	}

	public String updateResource() throws Exception {
		
		PrivilegeResourceItem sri = new PrivilegeResourceItem() ;
		sri.setId(resItemId) ;
		res.setResItem(sri) ;
		resMgr.updateResourceManager(res) ;

		return "queryByIdItem";
	}

	public PrivilegeResource getRes() {
		return res;
	}

	public void setRes(PrivilegeResource res) {
		this.res = res;
	}

	public void setResItemMgr(PrivilegeResourceItemManager resItemMgr) {
		this.resItemMgr = resItemMgr;
	}

	public void setResMgr(PrivilegeResourceManager resMgr) {
		this.resMgr = resMgr;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
